package com.ruoyi.parkingcar.controller;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.parkingcar.domain.ParkOrder;
import com.ruoyi.parkingcar.service.IParkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息管理Controller
 * 
 * @author 饼干
 * @date 2025-02-09
 */
@RestController
@RequestMapping("/parkingcar/order")
public class ParkOrderController extends BaseController
{
    @Autowired
    private IParkOrderService parkOrderService;

    private static int timm = 1000;

    /**
     * 查询订单信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(ParkOrder parkOrder)
    {
        startPage();
        List<ParkOrder> list = parkOrderService.selectParkOrderList(parkOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:order:export')")
    @Log(title = "订单信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParkOrder parkOrder)
    {
        List<ParkOrder> list = parkOrderService.selectParkOrderList(parkOrder);
        ExcelUtil<ParkOrder> util = new ExcelUtil<ParkOrder>(ParkOrder.class);
        util.exportExcel(response, list, "订单信息管理数据");
    }

    /**
     * 获取订单信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(parkOrderService.selectParkOrderByOrderId(orderId));
    }

    /**
     * 新增订单信息管理
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:order:add')")
    @Log(title = "订单信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParkOrder parkOrder)
    {
        return toAjax(parkOrderService.insertParkOrder(parkOrder));
    }

    /**
     * 修改订单信息管理
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:order:edit')")
    @Log(title = "订单信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParkOrder parkOrder)
    {
        return toAjax(parkOrderService.updateParkOrder(parkOrder));
    }

    /**
     * 删除订单信息管理
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:order:remove')")
    @Log(title = "订单信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(parkOrderService.deleteParkOrderByOrderIds(orderIds));
    }

    //查询车辆进场
    @GetMapping("/starter")
    public AjaxResult starter( String licensePlate){

        ParkOrder parkOrder = parkOrderService.selectCarByNumber(licensePlate);
        return AjaxResult.success("查询成功").put("data", parkOrder);
    }

    @PostMapping("/addOrder")
    public AjaxResult addOrder(@RequestBody ParkOrder parkOrder){

        String tiems = LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYYMMDDHHmmss"));
        timm +=1;
        tiems += timm;
        parkOrder.setOrderNo(tiems);
        System.out.println(parkOrder + "7----------");
        int Order = parkOrderService.insertOrder(parkOrder);
        return AjaxResult.success("成功").put("data", Order);

    }

    /**
     * Vant
     */
    @RequestMapping("/orderlist")
    public List<ParkOrder> select(ParkOrder parkOrder) {
        return parkOrderService.select(parkOrder);
    }


    //删除
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ParkOrder parkOrder) {
        int deleteResult = parkOrderService.delete(parkOrder);
        if (deleteResult != 0) {
            return AjaxResult.success("删除成功").put("delete",parkOrder.getOrderId());
        }else {
            return AjaxResult.error("删除失败");
        }
    }

    //阿里支付宝修改
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ParkOrder parkOrder) {
        int update = parkOrderService.updateParkOrder(parkOrder);
        if (update != 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    //Vant修改
    @PostMapping("/upById")
    public AjaxResult upById(@RequestBody ParkOrder parkOrder) {
         parkOrderService.updateOrderById(parkOrder);
        return AjaxResult.success("成功");
    }


    //添加出场时间
    @PostMapping("/costOrder")
    public AjaxResult costOrder(@RequestBody ParkOrder parkOrder ) {
        BigDecimal cost = parkOrderService.constOrder(parkOrder);
        return AjaxResult.success("成功").put("cost", cost);
    }

}
